<?php


namespace app\console\service\supplier;

use app\dao\goods\SupplierDao;
use app\model\Supplier;

class SupplierService
{
    /**
     * @var Supplier
     */
    private Supplier $supplier;

    public function __construct(Supplier $supplier)
    {
        $this->supplier = $supplier ;
    }

    public function create($data)
    {
        $supplierDao = new SupplierDao();

        return $supplierDao->save([
            'name'      => $data['name'],
            'province'  => $data['province'],
            'city'      => $data['city'],
            'district'  => $data['district'],
            'address'   => $data['address'],
            'linkman'   => $data['linkman'],
            'phone'     => $data['phone'],
            'wechat'    => $data['wechat'],
        ]);
    }

    public function update($data)
    {
        $supplierDao = new SupplierDao();

        if (!empty($data['name']))
        {
            $this->supplier->name = $data['name'];
        }
        if (!empty($data['province']))
        {
            $this->supplier->province = $data['province'];
        }
        if (!empty($data['city']))
        {
            $this->supplier->city = $data['city'];
        }
        if (!empty($data['district']))
        {
            $this->supplier->district = $data['district'];
        }else{
            //镇区为空
            $this->supplier->district = '';
        }
        if (!empty($data['address']))
        {
            $this->supplier->address = $data['address'];
        }
        if (!empty($data['linkman']))
        {
            $this->supplier->linkman = $data['linkman'];
        }
        if (!empty($data['phone']))
        {
            $this->supplier->phone = $data['phone'];
        }
        if (!empty($data['wechat']))
        {
            $this->supplier->wechat = $data['wechat'];
        }

        $supplierDao->update($this->supplier->toArray(), $this->supplier->id);

        return $supplierDao->getById($this->supplier->id);
    }

    public function delete()
    {
        $supplierDao = new SupplierDao();
        $supplierDao->deleteById($this->supplier->id);
    }

}